﻿using sydfdl.TMS.Model;
using sydfdl.TMS.Tool;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace sydfdl.TMS.BLL
{
    public class BLLT_B_Company
    {
        /// <summary>
        /// 获取公司类型
        /// </summary>
        /// <returns></returns>
        public IEnumerable<T_B_Dict> GetCompanyType()
        {
            List<T_B_Dict> list = new List<T_B_Dict>();

            string cmdText = "P_B_Company_GetCompanyType";

            using (SqlDataReader dr = SqlHelper.ExecuteReader(SqlHelper.tmsConn, CommandType.StoredProcedure, cmdText, null))
            {
                while (dr.Read() && dr.HasRows)
                {
                    T_B_Dict item = new T_B_Dict();
                    item.DID = Guid.Parse(dr["DID"].ToString());
                    item.DKey = dr["DKey"] == null ? string.Empty : dr["DKey"].ToString();
                    item.DValue = dr["DValue"] == null ? string.Empty : dr["DValue"].ToString();
                    list.Add(item);
                }

            }

            return list;
        }

        /// <summary>
        /// 获取公司列表
        /// </summary>
        /// <returns></returns>
        public DataTable GetAllCompanyList()
        {
            DataTable dt = new DataTable();

            try
            {
                dt = SqlHelper.ExecuteProcedureToDataSet("P_B_Company_GetAllCompanyList", null, "com").Tables[0];
            }
            catch (Exception ex)
            {
                dt = new DataTable();
            }

            return dt;
        }

        public string SaveCompanyData(IEnumerable<T_B_Company> add, IEnumerable<T_B_Company> edit, IEnumerable<T_B_Company> remove)
        {
            string addXML = EFHelp<T_B_Company>.GetXML(add);
            string editXML = EFHelp<T_B_Company>.GetXML(edit);
            string removeXML = EFHelp<T_B_Company>.GetXML(remove);

            SqlParameter[] para = { 
                                      new SqlParameter("@AddXML",SqlDbType.Xml,-1),
                                      new SqlParameter("@EditXML",SqlDbType.Xml,-1),
                                      new SqlParameter("@RemoveXML",SqlDbType.Xml,-1),
                                      new SqlParameter("@UserXML",SqlDbType.Xml,-1),
                                      new SqlParameter("@ReturnValue",SqlDbType.VarChar,100)
                                  };
            para[0].Value = addXML;
            para[1].Value = editXML;
            para[2].Value = removeXML;
            para[3].Value = null;
            para[4].Direction = ParameterDirection.Output;

            SqlHelper.ExecuteNonQuery(SqlHelper.tmsConn, CommandType.StoredProcedure, "P_B_Company_SaveData", para);

            return (para[4].Value ?? string.Empty).ToString();
        }
    }
}
